<template>
  <view class="cart-container" v-if="cart.length !== 0">
    <!-- 收件地址区域 -->
    <my-adress></my-adress>
    <!-- 商品列表的标题区域 -->
    <view class="cart-title">
      <!-- 图标 -->
      <uni-icons type="shop" size="18"></uni-icons>
      <!-- 文字 -->
      <text class="cart-title-text">购物车</text>
    </view>
    <!-- 商品区域 -->
    <uni-swipe-action>
      <block v-for="(item, index) in cart" :key="index">
        <uni-swipe-action-item :options="options" @click="removeGoods(item)">
          <my-goods :goods="item" :show-radio="true" @radio-changed="radioChangeHandler" @num-change="numChangeHandler">
          </my-goods>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action>
    <!--    <uni-swipe-action>
      <block v-for="(item, i) in cart" :key="i">
        <uni-swipe-action-item options="options" @click="removeGoods(item)">
          <my-goods :goods="item" :show-radio="true" @radio-changed="radioChangeHandler" @num-change="numChangeHandler">
          </my-goods>
        </uni-swipe-action-item>
      </block>
    </uni-swipe-action> -->
    <!-- 结算组件 -->
    <my-settle></my-settle>
  </view>
  <!-- 没有商品时 -->
  <view class="empty-container" v-else>
    <view class="empty-cart">
      <image src="/static/cart_empty@2x.png"></image>
      <text>空空如也</text>
    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar_badge.js'
  import {
    mapState,
    mapGetters,
    mapMutations
  } from 'vuex'
  export default {
    mixins: [badgeMix],
    computed: {
      ...mapState('m_cart', ['cart'])
    },
    data() {
      return {
        options: [{
          text: '删除', // 显示的文本内容
          style: {
            backgroundColor: '#C00000' // 按钮的背景颜色
          }
        }]
      }
    },
    methods: {
      ...mapMutations('m_cart', ['updateGoodsState', 'updateGoodsCount', 'removeGodds']),
      // 商品是否选择事件
      radioChangeHandler(e) {
        this.updateGoodsState(e)
      },
      // 数字输入框改变事件
      numChangeHandler(e) {
        this.updateGoodsCount(e)
      },
      // 删除商品
      removeGoods(e) {
        this.removeGodds(e)
      }
    }
  }
</script>

<style lang="scss">
  .cart-container {
    padding-bottom: 50px;
  }

  .cart-title {
    height: 40px;
    display: flex;
    align-items: center;
    padding-left: 5px;
    font-size: 14px;
    border-bottom: 1px solid #efefef;

    .cart-title-text {
      margin-left: 10px;
    }
  }

  .empty-container {
    height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;

    .empty-cart {
      display: flex;
      flex-direction: column;
      align-items: center;

      image {
        width: 90px;
        height: 90px;
      }

      text {
        margin-top: 15px;
        font-size: 12px;
        color: gray;
      }
    }
  }
</style>
